package com.chuantou.yuba.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.chuantou.yuba.R;
import com.chuantou.yuba.api.to.FriendTO;
import com.chuantou.yuba.api.to.IntegralTO;
import com.chuantou.yuba.util.DateUtil;

public class IntegralAdapter extends BaseAdapter {
	private Context mContext;
	private List<IntegralTO> mList = new ArrayList<IntegralTO>();

	public IntegralAdapter(Context context) {
		this.mContext = context;
	}

	public void add(IntegralTO to) {
		mList.add(to);
	}

	public void add(List<IntegralTO> list) {
		if (list != null && !list.isEmpty()) {
			mList.addAll(list);
		}
	}

	public void clear() {
		mList.clear();
	}

	@Override
	public int getCount() {
		return mList.size();
	}

	public void remove(FriendTO to) {
		mList.remove(to);
	}

	@Override
	public Object getItem(int position) {
		return mList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (convertView == null) {
			convertView = View.inflate(mContext, R.layout.integral_list, null);
		}
		IntegralTO integral = mList.get(position);
		TextView eventNameView = (TextView) convertView.findViewById(R.id.event_type);
		eventNameView.setText(integral.getChangeReason());
		TextView integralView = (TextView) convertView.findViewById(R.id.integral);
		int integralNum = integral.getIntegral();
		if (integral.getChangeMethod() == 2) {
			integralView.setText("-".concat(String.valueOf(integralNum)));
			integralView.setTextColor(Color.RED);
		} else {
			integralView.setText("+".concat(String.valueOf(integralNum)));
			integralView.setTextColor(mContext.getResources().getColor(R.color.integral_add_color));
		}
		TextView timeView = (TextView) convertView.findViewById(R.id.time);
		timeView.setText(DateUtil.getDateString(integral.getTime() * 1000, DateUtil.PATTERN_YMD));
		return convertView;
	}
}
